package org.christopher.jedisdemo;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Tuple;
import util.JedisUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisTest {

    /**
     * 测试Jedis操作string
     */
    @Test
    public void test01(){
        //建立连接
        Jedis jedis = new Jedis("localhost",6379);
        //添加数据
        jedis.set("name","林志玲");
        String name = jedis.get("name");
        System.out.println(name);
    }
    /**
     * 测试jedis设定过期时间
     */
    @Test
    public void test02() throws InterruptedException {
        //建立连接
        Jedis jedis = new Jedis("localhost",6379);
        //添加数据
        jedis.setex("username",5,"lzl");
        String username = jedis.get("username");
        System.out.println("实时获取"+username);
        //线程休眠6秒
        Thread.sleep(6000);
        String username2 = jedis.get("username");
        System.out.println("6秒以后"+username2);
    }

    @Test
    public void test03(){
        //建立连接
        Jedis jedis = new Jedis("localhost",6379);
        //添加数据
        jedis.hset("user","name","林志玲");
        jedis.hset("user","age","18");
        jedis.hset("user","desc","刘老师说好看");

        Map<String, String> user = jedis.hgetAll("user");

        for (String key:user.keySet()) {
            System.out.println(key+":"+user.get(key));
        }

        //jdk1.8以后的玩法
        user.keySet().forEach(key -> System.out.println(key+":"+user.get(key)));

    }
    @Test
    public void listTest(){
        List<String> strings = new ArrayList<>();
        strings.add("a");
        strings.add("b");
        strings.add("c");

//        for (String s:strings) {
//            System.out.println(s);
//        }
//        strings.forEach(s-> System.out.println(s));

        strings.forEach(System.out::println);
     }

    @Test
    public void test4(){
        //建立连接
        Jedis jedis = new Jedis();

        jedis.lpush("name","刘润泽","林志玲","贾玲 ");
        jedis.rpush("name","吴亦凡");

        List<String> names = jedis.lrange("name", 0, -1);

        names.forEach(System.out::println);

    }

    @Test
    public void test5(){
        //建立连接
        Jedis jedis = new Jedis();

        jedis.sadd("user","name","林志玲");

        jedis.sadd("user","age","18");
        jedis.sadd("user","desc","确实好看");

        Set<String> user = jedis.smembers("user");

        System.out.println(user);


    }


    @Test
    public void test6(){
        //建立连接
        Jedis jedis = new Jedis();

        jedis.zadd("stu",0,"杨紫彤");
        jedis.zadd("stu",100,"杨子童");

        Set<String> stu = jedis.zrange("stu", 0, -1);
        System.out.println(stu);

        Set<Tuple> stu1 = jedis.zrangeWithScores("stu", 0, -1);
        System.out.println(stu1);
    }

    @Test
    public void test7(){

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        //最大连接数
        jedisPoolConfig.setMaxTotal(50);
        //最大空闲连接
        jedisPoolConfig.setMaxIdle(10);

        //创建连接池对象
        JedisPool jedisPool = new JedisPool();
        //通过连接池获取连接
        Jedis jedis = jedisPool.getResource();

        jedis.set("name","刘润泽");
        System.out.println(jedis.get("name"));
        //归还连接到连接池。
        jedis.close();

    }

    @Test
    public void test08(){
        Jedis jedis = JedisUtil.getJedis();
        jedis.set("name","林志玲");
        System.out.println(jedis.get("name"));
    }













}
